home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / doslynx / src / html.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  32.1 KB  |  1,212 lines

  1. /*        Structured stream to Rich hypertext converter
  2. **        ============================================
  3. **
  4. **    This generates of a hypertext object.  It converts from the
  5. **    structured stream interface fro HTMl events into the style-
  6. **    oriented iunterface of the HText.h interface.  This module is
  7. **    only used in clients and shouldnot be linked into servers.
  8. **
  9. **    Override this module if making a new GUI browser.
  10. **
  11. */
  12. #include"capalloc.h"
  13. #include "HTML.h"
  14. #include"globals.h"
  15.  
  16. /* #define CAREFUL         Check nesting here notreally necessary */
  17.  
  18. #include <ctype.h>
  19. #include <stdio.h>
  20. #include"capstdio.h"
  21.  
  22. #include "HTAtom.h"
  23. #include "HTChunk.h"
  24. #include "HText.h"
  25. #include "HTStyle.h"
  26.  
  27. #include "HTAlert.h"
  28. #include "HTMLGen.h"
  29. #include "HTParse.h"
  30.  
  31. extern HTStyleSheet * styleSheet;    /* Application-wide */
  32.  
  33. /*    Module-wide style cache
  34. */
  35. PRIVATE int         got_styles = 0;
  36. PRIVATE HTStyle *styles[HTML_ELEMENTS];
  37. PRIVATE HTStyle *default_style;
  38.  
  39.  
  40. /*        HTML Object
  41. **        -----------
  42. */
  43. #define MAX_NESTING 200        /* Should be checked by parser */
  44.  
  45. typedef struct _stack_element {
  46.         HTStyle *    style;
  47.     int        tag_number;
  48. } stack_element;
  49.  
  50. struct _HTStructured {
  51.     CONST HTStructuredClass *     isa;
  52.     HTParentAnchor *         node_anchor;
  53.     HText *             text;
  54.  
  55.     HTStream*            target;            /* Output stream */
  56.     HTStreamClass        targetClass;        /* Output routines */
  57.  
  58.     HTChunk             title;        /* Grow by 128 */
  59.     
  60.     char *            comment_start;    /* for literate programming */
  61.     char *            comment_end;
  62.  
  63.     HTTag *            current_tag;
  64.     BOOL            style_change;
  65.     HTStyle *            new_style;
  66.     HTStyle *            old_style;
  67.     BOOL            in_word;  /* Have just had a non-white char */
  68.     stack_element     stack[MAX_NESTING];
  69.     stack_element     *sp;        /* Style stack pointer */
  70. };
  71.  
  72. struct _HTStream {
  73.     CONST HTStreamClass *    isa;
  74.     /* .... */
  75. };
  76.  
  77. /*        Forward declarations of routines
  78. */
  79. PRIVATE void get_styles NOPARAMS;
  80.  
  81.  
  82. PRIVATE void actually_set_style PARAMS((HTStructured * me));
  83. PRIVATE void change_paragraph_style PARAMS((HTStructured * me, HTStyle * style));
  84.  
  85. /*    Style buffering avoids dummy paragraph begin/ends.
  86. */
  87. #define UPDATE_STYLE if (me->style_change) { actually_set_style(me); }
  88.  
  89.  
  90. #ifdef OLD_CODE
  91. /* The following accented characters are from peter Flynn, curia project */
  92.  
  93. /* these ifdefs don't solve the problem of a simple terminal emulator
  94. ** with a different character set to the client machine. But nothing does,
  95. ** except looking at the TERM setting */
  96.  
  97.  
  98.         { "ocus" , "&" },       /* for CURIA */
  99. #ifdef IBMPC
  100.         { "aacute" , "\240" },    /* For PC display */
  101.         { "eacute" , "\202" },
  102.         { "iacute" , "\241" },
  103.         { "oacute" , "\242" },
  104.         { "uacute" , "\243" },
  105.         { "Aacute" , "\101" },
  106.         { "Eacute" , "\220" },
  107.         { "Iacute" , "\111" },
  108.         { "Oacute" , "\117" },
  109.         { "Uacute" , "\125" },
  110. #else
  111.     { "aacute" , "\341" },    /* Works for openwindows -- Peter Flynn */
  112.     { "eacute" , "\351" },
  113.     { "iacute" , "\355" },
  114.     { "oacute" , "\363" },
  115.     { "uacute" , "\372" },
  116.     { "Aacute" , "\301" },
  117.     { "Eacute" , "\310" },
  118.     { "Iacute" , "\315" },
  119.     { "Oacute" , "\323" },
  120.     { "Uacute" , "\332" },
  121. #endif
  122.     { 0,    0 }  /* Terminate list */
  123. };
  124. #endif
  125.  
  126.  
  127. /*     Entity values -- for ISO Latin 1 local representation
  128. **
  129. **    This MUST match exactly the table referred to in the DTD!
  130. */
  131. #ifndef MSDOS
  132. static char * ISO_Latin1[] = {
  133.     "\306",    /* capital AE diphthong (ligature) */
  134.     "\301",    /* capital A, acute accent */
  135.     "\302",    /* capital A, circumflex accent */
  136.     "\300",    /* capital A, grave accent */
  137.     "\305",    /* capital A, ring */
  138.     "\303",    /* capital A, tilde */
  139.     "\304",    /* capital A, dieresis or umlaut mark */
  140.     "\307",    /* capital C, cedilla */
  141.     "\320",    /* capital Eth, Icelandic */
  142.     "\311",    /* capital E, acute accent */
  143.     "\312",    /* capital E, circumflex accent */
  144.     "\310",    /* capital E, grave accent */
  145.     "\313",    /* capital E, dieresis or umlaut mark */
  146.     "\315",    /* capital I, acute accent */
  147.     "\316",    /* capital I, circumflex accent */
  148.     "\314",    /* capital I, grave accent */
  149.     "\317",    /* capital I, dieresis or umlaut mark */
  150.     "\321",    /* capital N, tilde */
  151.     "\323",    /* capital O, acute accent */
  152.       "\324",    /* capital O, circumflex accent */ 
  153.       "\322",    /* capital O, grave accent */ 
  154.       "\330",    /* capital O, slash */ 
  155.     "\325",    /* capital O, tilde */
  156.       "\326",    /* capital O, dieresis or umlaut mark */ 
  157.       "\336",    /* capital THORN, Icelandic */ 
  158.       "\332",    /* capital U, acute accent */ 
  159.       "\333",    /* capital U, circumflex accent */ 
  160.     "\331",    /* capital U, grave accent */
  161.       "\334",    /* capital U, dieresis or umlaut mark */ 
  162.       "\335",    /* capital Y, acute accent */ 
  163.       "\341",    /* small a, acute accent */ 
  164.       "\342",    /* small a, circumflex accent */ 
  165.       "\346",    /* small ae diphthong (ligature) */ 
  166.       "\340",    /* small a, grave accent */ 
  167.     "\046",    /* ampersand */
  168.     "\345",    /* small a, ring */
  169.       "\343",    /* small a, tilde */ 
  170.       "\344",    /* small a, dieresis or umlaut mark */ 
  171.       "\347",    /* small c, cedilla */ 
  172.       "\351",    /* small e, acute accent */ 
  173.       "\352",    /* small e, circumflex accent */ 
  174.       "\350",    /* small e, grave accent */ 
  175.       "\360",    /* small eth, Icelandic */ 
  176.       "\353",    /* small e, dieresis or umlaut mark */ 
  177.       "\076",    /* greater than */ 
  178.     "\355",    /* small i, acute accent */
  179.       "\356",    /* small i, circumflex accent */ 
  180.     "\354",    /* small i, grave accent */
  181.       "\357",    /* small i, dieresis or umlaut mark */ 
  182.       "\074",    /* less than */ 
  183.       "\361",    /* small n, tilde */ 
  184.       "\363",    /* small o, acute accent */ 
  185.       "\364",    /* small o, circumflex accent */ 
  186.       "\362",    /* small o, grave accent */ 
  187.       "\370",    /* small o, slash */ 
  188.       "\365",    /* small o, tilde */ 
  189.       "\366",    /* small o, dieresis or umlaut mark */ 
  190.       "\337",    /* small sharp s, German (sz ligature) */ 
  191.       "\376",    /* small thorn, Icelandic */ 
  192.       "\372",    /* small u, acute accent */ 
  193.       "\373",    /* small u, circumflex accent */ 
  194.       "\371",    /* small u, grave accent */ 
  195.     "\374",    /* small u, dieresis or umlaut mark */
  196.     "\375",    /* small y, acute accent */
  197.       "\377",    /* small y, dieresis or umlaut mark */ 
  198. };
  199. #else
  200. static char * ISO_Latin1[] = {
  201.     "\222",    /* capital AE diphthong (ligature) */
  202.     "\265",    /* capital A, acute accent */
  203.     "\266",    /* capital A, circumflex accent */
  204.     "\267",    /* capital A, grave accent */
  205.     "\217",    /* capital A, ring */
  206.     "\307",    /* capital A, tilde */
  207.     "\216",    /* capital A, dieresis or umlaut mark */
  208.     "\200",    /* capital C, cedilla */
  209.     "\321",    /* capital Eth, Icelandic */
  210.     "\220",    /* capital E, acute accent */
  211.     "\322",    /* capital E, circumflex accent */
  212.     "\324",    /* capital E, grave accent */
  213.     "\323",    /* capital E, dieresis or umlaut mark */
  214.     "\326",    /* capital I, acute accent */
  215.     "\327",    /* capital I, circumflex accent */
  216.     "\336",    /* capital I, grave accent */
  217.     "\330",    /* capital I, dieresis or umlaut mark */
  218.     "\245",    /* capital N, tilde */
  219.     "\340",    /* capital O, acute accent */
  220.     "\342",    /* capital O, circumflex accent */
  221.     "\343",    /* capital O, grave accent */
  222.     "\235",    /* capital O, slash */
  223.     "\345",    /* capital O, tilde */
  224.     "\231",    /* capital O, dieresis or umlaut mark */
  225.     "\350",    /* capital THORN, Icelandic */
  226.     "\351",    /* capital U, acute accent */
  227.     "\352",    /* capital U, circumflex accent */
  228.     "\353",    /* capital U, grave accent */
  229.     "\232",    /* capital U, dieresis or umlaut mark */
  230.     "\355",    /* capital Y, acute accent */
  231.     "\240",    /* small a, acute accent */
  232.     "\203",    /* small a, circumflex accent */
  233.     "\221",    /* small ae diphthong (ligature) */
  234.     "\205",    /* small a, grave accent */
  235.     "\046",    /* ampersand */
  236.     "\206",    /* small a, ring */
  237.     "\306",    /* small a, tilde */
  238.     "\204",    /* small a, dieresis or umlaut mark */
  239.     "\207",    /* small c, cedilla */
  240.     "\202",    /* small e, acute accent */
  241.     "\210",    /* small e, circumflex accent */
  242.     "\212",    /* small e, grave accent */
  243.     "\320",    /* small eth, Icelandic */
  244.     "\211",    /* small e, dieresis or umlaut mark */
  245.     "\076",    /* greater than */
  246.     "\241",    /* small i, acute accent */
  247.     "\214",    /* small i, circumflex accent */
  248.     "\215",    /* small i, grave accent */
  249.     "\213",    /* small i, dieresis or umlaut mark */
  250.     "\074",    /* less than */
  251.     "\244",    /* small n, tilde */
  252.     "\242",    /* small o, acute accent */
  253.     "\223",    /* small o, circumflex accent */
  254.     "\225",    /* small o, grave accent */
  255.     "\233",    /* small o, slash */
  256.     "\344",    /* small o, tilde */
  257.     "\224",    /* small o, dieresis or umlaut mark */
  258.     "\341",    /* small sharp s, German (sz ligature) */
  259.     "\347",    /* small thorn, Icelandic */
  260.     "\243",    /* small u, acute accent */
  261.     "\226",    /* small u, circumflex accent */
  262.     "\227",    /* small u, grave accent */
  263.     "\201",    /* small u, dieresis or umlaut mark */
  264.     "\354",    /* small y, acute accent */
  265.     "\230",    /* small y, dieresis or umlaut mark */
  266. };
  267. #endif /* MSDOS */
  268.  
  269. /*     Entity values -- for NeXT local representation
  270. **
  271. **    This MUST match exactly the table referred to in the DTD!
  272. **
  273. */
  274. static char * NeXTCharacters[] = {
  275.     "\341",    /* capital AE diphthong (ligature)     */
  276.     "\202",    /* capital A, acute accent        */
  277.     "\203",    /* capital A, circumflex accent     */
  278.     "\201",    /* capital A, grave accent         */
  279.     "\206",    /* capital A, ring             */
  280.     "\204",    /* capital A, tilde             */
  281.     "\205",    /* capital A, dieresis or umlaut mark    */
  282.     "\207",    /* capital C, cedilla             */
  283.     "\220",    /* capital Eth, Icelandic         */
  284.     "\211",    /* capital E, acute accent                 */
  285.     "\212",    /* capital E, circumflex accent             */
  286.     "\210",    /* capital E, grave accent                 */
  287.     "\213",    /* capital E, dieresis or umlaut mark             */
  288.     "\215",    /* capital I, acute accent                 */
  289.     "\216",    /* capital I, circumflex accent     these are    */
  290.     "\214",    /* capital I, grave accent        ISO -100 hex    */
  291.     "\217",    /* capital I, dieresis or umlaut mark            */
  292.     "\221",    /* capital N, tilde                     */
  293.     "\223",    /* capital O, acute accent                 */
  294.     "\224",    /* capital O, circumflex accent             */
  295.     "\222",    /* capital O, grave accent                 */
  296.     "\351",    /* capital O, slash         'cept this */
  297.     "\225",    /* capital O, tilde                     */
  298.     "\226",    /* capital O, dieresis or umlaut mark            */
  299.     "\234",    /* capital THORN, Icelandic */
  300.     "\230",    /* capital U, acute accent */
  301.     "\231",    /* capital U, circumflex accent */
  302.     "\227",    /* capital U, grave accent */
  303.     "\232",    /* capital U, dieresis or umlaut mark */
  304.     "\233",    /* capital Y, acute accent */
  305.     "\326",    /* small a, acute accent */
  306.     "\327",    /* small a, circumflex accent */
  307.     "\361",    /* small ae diphthong (ligature) */
  308.     "\325",    /* small a, grave accent */
  309.     "\046",    /* ampersand */
  310.       "\332",    /* small a, ring */ 
  311.       "\330",    /* small a, tilde */ 
  312.       "\331",    /* small a, dieresis or umlaut mark */ 
  313.       "\333",    /* small c, cedilla */ 
  314.       "\335",    /* small e, acute accent */ 
  315.       "\336",    /* small e, circumflex accent */ 
  316.       "\334",    /* small e, grave accent */ 
  317.       "\346",    /* small eth, Icelandic     */ 
  318.       "\337",    /* small e, dieresis or umlaut mark */ 
  319.       "\076",    /* greater than */ 
  320.       "\342",    /* small i, acute accent */ 
  321.       "\344",    /* small i, circumflex accent */ 
  322.       "\340",    /* small i, grave accent */ 
  323.       "\345",    /* small i, dieresis or umlaut mark */ 
  324.     "\074",    /* less than */
  325.     "\347",    /* small n, tilde */
  326.       "\355",    /* small o, acute accent */ 
  327.       "\356",    /* small o, circumflex accent */ 
  328.       "\354",    /* small o, grave accent */ 
  329.       "\371",    /* small o, slash */ 
  330.       "\357",    /* small o, tilde */ 
  331.       "\360",    /* small o, dieresis or umlaut mark */ 
  332.       "\373",    /* small sharp s, German (sz ligature) */ 
  333.     "\374",    /* small thorn, Icelandic */
  334.       "\363",    /* small u, acute accent */ 
  335.       "\364",    /* small u, circumflex accent */ 
  336.     "\362",    /* small u, grave accent */
  337.       "\366",    /* small u, dieresis or umlaut mark */ 
  338.     "\367",    /* small y, acute accent */
  339.       "\375",    /* small y, dieresis or umlaut mark */ 
  340. };
  341.  
  342. /*     Entity values -- for IBM/PC Code Page 850 (International)
  343. **
  344. **    This MUST match exactly the table referred to in the DTD!
  345. **
  346. */
  347. /* @@@@@@@@@@@@@@@@@ TBD */
  348.  
  349.  
  350.  
  351. /*        Set character set
  352. **        ----------------
  353. */
  354.  
  355. PRIVATE char** p_entity_values = ISO_Latin1;    /* Pointer to translation */
  356.  
  357. PUBLIC void HTMLUseCharacterSet ARGS1(HTMLCharacterSet, i)
  358. {
  359.     p_entity_values = (i == HTML_NEXT_CHARS) ? NeXTCharacters
  360.                              : ISO_Latin1;
  361. }
  362.  
  363.  
  364. /*        Flattening the style structure
  365. **        ------------------------------
  366. **
  367. On the NeXT, and on any read-only browser, it is simpler for the text to have
  368. a sequence of styles, rather than a nested tree of styles. In this
  369. case we have to flatten the structure as it arrives from SGML tags into
  370. a sequence of styles.
  371. */
  372.  
  373. /*        If style really needs to be set, call this
  374. */
  375. PRIVATE void actually_set_style ARGS1(HTStructured *, me)
  376. {
  377.     if (!me->text) {            /* First time through */
  378.         me->text = HText_new2(me->node_anchor, me->target);
  379.         HText_beginAppend(me->text);
  380.         HText_setStyle(me->text, me->new_style);
  381.         me->in_word = NO;
  382.     } else {
  383.         HText_setStyle(me->text, me->new_style);
  384.     }
  385.     me->old_style = me->new_style;
  386.     me->style_change = NO;
  387. }
  388.  
  389. /*      If you THINK you need to change style, call this
  390. */
  391.  
  392. PRIVATE void change_paragraph_style ARGS2(HTStructured *, me, HTStyle *,style)
  393. {
  394.     if (me->new_style!=style) {
  395.         me->style_change = YES;
  396.     me->new_style = style;
  397.     }
  398.     me->in_word = NO;
  399. }
  400.  
  401. /*_________________________________________________________________________
  402. **
  403. **            A C T I O N     R O U T I N E S
  404. */
  405.  
  406. /*    Character handling
  407. **    ------------------
  408. */
  409.  
  410. PRIVATE void HTML_put_character ARGS2(HTStructured *, me, char, c)
  411. {
  412.  
  413.     switch (me->sp[0].tag_number) {
  414.     case HTML_COMMENT:
  415.     break;                    /* Do Nothing */
  416.  
  417.     case HTML_TITLE:
  418.     HTChunkPutc(&me->title, c);
  419.     break;
  420.  
  421.  
  422.     case HTML_LISTING:                /* Litteral text */
  423.     case HTML_XMP:
  424.     case HTML_PLAINTEXT:
  425.     case HTML_PRE:
  426. /*    We guarrantee that the style is up-to-date in begin_litteral
  427. */
  428.     HText_appendCharacter(me->text, c);
  429.     break;
  430.     
  431.     default:                    /* Free format text */
  432.     if (me->style_change) {
  433.         if ((c=='\n') || (c==' ')) return;    /* Ignore it */
  434.         UPDATE_STYLE;
  435.     }
  436.     if (c=='\n') {
  437.         if (me->in_word) {
  438.         HText_appendCharacter(me->text, ' ');
  439.         me->in_word = NO;
  440.         }
  441.     } else {
  442.         HText_appendCharacter(me->text, c);
  443.         me->in_word = YES;
  444.     }
  445.     } /* end switch */
  446. }
  447.  
  448.  
  449.  
  450. /*    String handling
  451. **    ---------------
  452. **
  453. **    This is written separately from put_character becuase the loop can
  454. **    in some cases be promoted to a higher function call level for speed.
  455. */
  456. PRIVATE void HTML_put_string ARGS2(HTStructured *, me, CONST char*, s)
  457. {
  458.  
  459.     switch (me->sp[0].tag_number) {
  460.     case HTML_COMMENT:
  461.         break;                    /* Do Nothing */
  462.     
  463.     case HTML_TITLE:    
  464.         HTChunkPuts(&me->title, s);
  465.     break;
  466.  
  467.     
  468.     case HTML_LISTING:                /* Litteral text */
  469.     case HTML_XMP:
  470.     case HTML_PLAINTEXT:
  471.     case HTML_PRE:
  472.  
  473. /*    We guarrantee that the style is up-to-date in begin_litteral
  474. */
  475.     HText_appendText(me->text, s);
  476.     break;
  477.  
  478.     default:                    /* Free format text */
  479.     {
  480.         CONST char *p = s;
  481.         if (me->style_change) {
  482.         for (; *p && ((*p=='\n') || (*p==' ')); p++)  ;  /* Ignore leaders */
  483.         if (!*p) return;
  484.         UPDATE_STYLE;
  485.         }
  486.         for(; *p; p++) {
  487.         if (me->style_change) {
  488.             if ((*p=='\n') || (*p==' ')) continue;  /* Ignore it */
  489.             UPDATE_STYLE;
  490.         }
  491.         if (*p=='\n') {
  492.             if (me->in_word) {
  493.             HText_appendCharacter(me->text, ' ');
  494.             me->in_word = NO;
  495.             }
  496.         } else {
  497.             HText_appendCharacter(me->text, *p);
  498.             me->in_word = YES;
  499.         }
  500.         } /* for */
  501.     }
  502.     } /* end switch */
  503. }
  504.  
  505.  
  506. /*    Buffer write
  507. **    ------------
  508. */
  509. PRIVATE void HTML_write ARGS3(HTStructured *, me, CONST char*, s, int, l)
  510. {
  511.     CONST char* p;
  512.     CONST char* e = s+l;
  513.     for (p=s; s<e; p++) HTML_put_character(me, *p);
  514. }
  515.  
  516.  
  517. /*    Start Element
  518. **    -------------
  519. */
  520. PRIVATE void HTML_start_element ARGS4(
  521.     HTStructured *,     me,
  522.     int,        element_number,
  523.     CONST BOOL*,         present,
  524.     CONST char **,    value)
  525. {
  526. #ifdef MSDOSMEM
  527.     {
  528.         extern void *vp_msdosmem;
  529.         extern void **vpp_msdosmem;
  530.         present = (BOOL *)vp_msdosmem;
  531.         value = (char **)vpp_msdosmem;
  532.     }
  533. #endif /* MSDOSMEM */
  534.  
  535.     switch (element_number) {
  536.  
  537.     /*
  538.      *    Forms stuff.
  539.      */
  540.     case HTML_FORM:    {
  541.         /*
  542.          *    Handle form arguments.
  543.          *    Right now, we don't handle the ENCTYPE.
  544.          */
  545.         auto char *cp_action = NULL;
  546.         auto char *cp_method = NULL;
  547.  
  548.         /*
  549.          *    Forms can't be nested.
  550.          */
  551.         if(HText_inForm(me->text) == TRUE)    {
  552. #ifndef RELEASE
  553.             if(TRACE)    {
  554.                 fprintf(stderr, "Forms can't be nested.\n");
  555.             }
  556. #endif /* RELEASE */
  557.             break;
  558.         }
  559.  
  560.         /*
  561.          *    Ensure the proper style in the form.
  562.          */
  563.         UPDATE_STYLE;
  564.  
  565.         /*
  566.          *    Was an action server named?
  567.          */
  568.         if(present[HTML_FORM_ACTION])    {
  569.             /*
  570.              *    Copy over and simplfy the action
  571.              *    server.
  572.              */
  573.             StrAllocCopy(cp_action, value[HTML_FORM_ACTION]);
  574.             HTSimplify(cp_action);
  575.         }
  576.  
  577.         /*
  578.          *    Was the method to be used named?
  579.          */
  580.         if(present[HTML_FORM_METHOD])    {
  581.             StrAllocCopy(cp_method, value[HTML_FORM_METHOD]);
  582.         }
  583.  
  584.         /*
  585.          *    Call GridText to begin the creation of a form.
  586.          */
  587.         HText_beginForm(me->text, cp_action, cp_method);
  588.  
  589.         /*
  590.          *    Free any possibly allocated memory.
  591.          */
  592.         if(cp_action != NULL)    {
  593.             free(cp_action);
  594.         }
  595.         if(cp_method != NULL)    {
  596.             free(cp_method);
  597.         }
  598. #ifndef RELEASE
  599.         if(TRACE)    {
  600.             fprintf(stderr, "Form start created.\n");
  601.         }
  602. #endif /* RELEASE */
  603.         break;
  604.     }
  605.     case HTML_INPUT:    {
  606.         auto char *cp_type = NULL;
  607.         auto char *cp_name = NULL;
  608.  
  609.         /*
  610.          *    First off, we must be inside of a form for this to
  611.          *    work.
  612.          */
  613.         if(HText_inForm(me->text) == FALSE)    {
  614. #ifndef RELEASE
  615.             if(TRACE)    {
  616.                 fprintf(stderr, "Input not inside form.\n");
  617.             }
  618. #endif /* RELEASE */
  619.             break;
  620.         }
  621.  
  622.         /*
  623.          *    Ensure the proper style in the form.
  624.          */
  625.         UPDATE_STYLE;
  626.  
  627.         /*
  628.          *    Type specifies the type of input to be retrieved from
  629.          *        the user.
  630.          *    text        Text entry field and is the default.
  631.          *    password    As text, but input is * out.
  632.          *    checkbox    Single toggle, on or off.
  633.          *    radio        Single toggle, on or off but grouped
  634.          *                with radios of the same name.
  635.          *    submit        Pushbutton to send the form.
  636.          *    reset        Pushbutton to revert form to original
  637.          *                values.
  638.          */
  639.         if(present[HTML_INPUT_TYPE])    {
  640.             cp_type = value[HTML_INPUT_TYPE];
  641.         }
  642.         else    {
  643.             cp_type = "text";
  644.         }
  645.         /*
  646.          *    Convert the type to it's enumeration.
  647.          */
  648.  
  649.         /*
  650.          *    There must be a name to each input element except
  651.          *        submit and reset.
  652.          *    Names are not displayed.
  653.          *     Used to put together the query string that gets sent
  654.          *        to the server when the form is submitted.
  655.          */
  656.         if(present[HTML_INPUT_NAME])    {
  657.         }
  658.         else    {
  659.         }
  660.  
  661.         break;
  662.     }
  663.     case HTML_OPTION:    {
  664.         /*
  665.          *    Ensure the proper style in the form.
  666.          */
  667.         UPDATE_STYLE;
  668.         break;
  669.     }
  670.     case HTML_SELECT:    {
  671.         /*
  672.          *    Ensure the proper style in the form.
  673.          */
  674.         UPDATE_STYLE;
  675.         break;
  676.     }
  677.     case HTML_TEXTAREA:    {
  678.         /*
  679.          *    Ensure the proper style in the form.
  680.          */
  681.         UPDATE_STYLE;
  682.         break;
  683.     }
  684.  
  685.     case HTML_A:
  686.         /*
  687.          *    First, we will not start any new anchors if we
  688.          *    are really low on memory.  Check.
  689.          */
  690.         if(really_safe_pool[Stop_Anchors] == NULL)    {
  691.             usi_skipped++;
  692.             break;
  693.         }
  694.         else    {
  695.             HTChildAnchor * source;
  696.             char * href = NULL;
  697.  
  698.             /*
  699.              *    set to know that we started an anchor, is OK
  700.              *    to end the anchor.
  701.              */
  702.             c_DidStartAnchor = 1;
  703.  
  704.             if(present[HTML_A_HREF])    {
  705.                 StrAllocCopy(href, value[HTML_A_HREF]);
  706.                 HTSimplify(href);
  707.             }
  708.  
  709.             source = HTAnchor_findChildAndLink(me->node_anchor,
  710.                 present[HTML_A_NAME] ? value[HTML_A_NAME] : 0,
  711.                 present[HTML_A_HREF] ? href : 0, present[
  712.                 HTML_A_TYPE] && value[HTML_A_TYPE] ?
  713.                 (HTLinkType*)HTAtom_for(value[HTML_A_TYPE]) :
  714.                 0);
  715.  
  716.             if(present[HTML_A_TITLE] && value[HTML_A_TITLE])    {
  717.                 HTParentAnchor *dest = HTAnchor_parent(
  718.                     HTAnchor_followMainLink((HTAnchor*)
  719.                     source));
  720.  
  721.             if (!HTAnchor_title(dest))
  722.                 HTAnchor_setTitle(dest, value[HTML_A_TITLE]);
  723.         }
  724.         UPDATE_STYLE;
  725.         HText_beginAnchor(me->text, source);
  726.     }
  727.     break;
  728.  
  729.     case HTML_TITLE:
  730.     HTChunkClear(&me->title);
  731.     break;
  732.  
  733.     case HTML_NEXTID:
  734.     /* if (present[NEXTID_N] && value[NEXTID_N])
  735.         HText_setNextId(me->text, atoi(value[NEXTID_N])); */
  736.     break;
  737.  
  738.     case HTML_ISINDEX:
  739.     if(present != NULL && present[HTML_ISINDEX_ACTION] != NO)    {
  740. #ifndef RELEASE
  741.         if(TRACE)    {
  742.             fprintf(stderr, "HTML:  1 HTAnchor_setIndex(%p, %s)\n",
  743.                 (void *)(me->node_anchor), (char *)(value[
  744.                 HTML_ISINDEX_ACTION]));
  745.         }
  746. #endif /* RELEASE */
  747.         HTAnchor_setIndex(me->node_anchor, value[HTML_ISINDEX_ACTION]);
  748.     }
  749.     else    {
  750. #ifndef RELEASE
  751.         if(TRACE)    {
  752.             fprintf(stderr, "HTML:  2 HTAnchor_setIndex(%p, %s)\n",
  753.                 (void *)(me->node_anchor), me->node_anchor->
  754.                 address);
  755.         }
  756. #endif /* RELEASE */
  757.         HTAnchor_setIndex(me->node_anchor, me->node_anchor->address);
  758.     }
  759.     break;
  760.  
  761.     case HTML_BR:
  762.     UPDATE_STYLE;
  763.     HText_appendCharacter(me->text, '\n');
  764.     me->in_word = NO;
  765.     break;
  766.  
  767.     case HTML_HR:
  768.     UPDATE_STYLE;
  769.     HText_appendCharacter(me->text, '\n');
  770.     {
  771.         unsigned short int usi_count;
  772.         for(usi_count = 0; usi_count < 35; usi_count++)
  773.             HText_appendCharacter(me->text, '-');
  774.     }
  775.     HText_appendCharacter(me->text, '\n');
  776.     me->in_word = NO;
  777.     break;
  778.  
  779.     case HTML_P:
  780.     UPDATE_STYLE;
  781.     HText_appendParagraph(me->text);
  782.     me->in_word = NO;
  783.     break;
  784.  
  785.     case HTML_DL:
  786.     change_paragraph_style(me, present && present[DL_COMPACT]
  787.         ? styles[HTML_DLC]
  788.         : styles[HTML_DL]);
  789.     break;
  790.  
  791.     case HTML_DT:
  792.     if (!me->style_change) {
  793.         HText_appendParagraph(me->text);
  794.         me->in_word = NO;
  795.     }
  796.     break;
  797.  
  798.     case HTML_DD:
  799.     UPDATE_STYLE;
  800. /*    HTML_put_character(me, '\t');    Just tab, format: doesn't work well */
  801.     HText_appendCharacter(me->text, '\t');
  802.     me->in_word = NO;
  803.     break;
  804.  
  805.     case HTML_UL:
  806.     case HTML_OL:
  807.     case HTML_MENU:
  808.     case HTML_DIR:
  809.     change_paragraph_style(me, styles[element_number]);
  810.     break;
  811.  
  812.     case HTML_LI:
  813.     UPDATE_STYLE;
  814.     HText_appendParagraph(me->text);
  815.     me->in_word = NO;
  816.     break;
  817.  
  818.     case HTML_LISTING:                /* Litteral text */
  819.     case HTML_XMP:
  820.     case HTML_PLAINTEXT:
  821.     case HTML_PRE:
  822.     change_paragraph_style(me, styles[element_number]);
  823.     UPDATE_STYLE;
  824.     if (me->comment_end)
  825.         HText_appendText(me->text, me->comment_end);
  826.     break;
  827.  
  828.     case HTML_IMG:            /* Images -- display text */
  829.     if(present[HTML_IMG_ALT])    {
  830.         if(value[HTML_IMG_ALT] != NULL)    {
  831.             if(*(value[HTML_IMG_ALT]) != '\0')    {
  832.                 HTML_put_character(me, ' ');
  833.                 HTML_put_string(me, value[HTML_IMG_ALT]);
  834.                 HTML_put_character(me, ' ');
  835.             }
  836.         }
  837.         else    {
  838.             HTML_put_string(me, " [IMAGE] ");
  839.         }
  840.     }
  841.     else    {
  842.         HTML_put_string(me, " [IMAGE] ");
  843.     }
  844.     break;
  845.  
  846.     case HTML_HTML:            /* Ignore these altogether */
  847.     case HTML_HEAD:
  848.     case HTML_BODY:
  849.  
  850.  
  851.     case HTML_TT:            /* Physical character highlighting */
  852.     case HTML_B:            /* Currently ignored */
  853.     case HTML_I:
  854.     case HTML_U:
  855.  
  856.     case HTML_EM:            /* Logical character highlighting */
  857.     case HTML_STRONG:            /* Currently ignored */
  858.     case HTML_CODE:
  859.     case HTML_SAMP:
  860.     case HTML_KBD:
  861.     case HTML_VAR:
  862.     case HTML_DFN:
  863.     case HTML_CITE:
  864.         break;
  865.     
  866.     case HTML_H1:            /* paragraph styles */
  867.     case HTML_H2:
  868.     case HTML_H3:
  869.     case HTML_H4:
  870.     case HTML_H5:
  871.     case HTML_H6:
  872.     case HTML_H7:
  873.     case HTML_ADDRESS:
  874.     case HTML_BLOCKQUOTE:
  875.     change_paragraph_style(me, styles[element_number]);    /* May be postponed */
  876.     break;
  877.  
  878.     } /* end switch */
  879.  
  880.     if (HTML_dtd.tags[element_number].contents!= SGML_EMPTY) {
  881.     if (me->sp == me->stack) {
  882.         fprintf(stderr, "HTML: ****** Maximum nesting of %d exceded!\n",
  883.         MAX_NESTING);
  884.         return;
  885.     }
  886.     --(me->sp);
  887.     me->sp[0].style = me->new_style;    /* Stack new style */
  888.     me->sp[0].tag_number = element_number;
  889.     }
  890. }
  891.  
  892.  
  893. /*        End Element
  894. **        -----------
  895. **
  896. */
  897. /*    When we end an element, the style must be returned to that
  898. **    in effect before that element.  Note that anchors (etc?)
  899. **    don't have an associated style, so that we must scan down the
  900. **    stack for an element with a defined style. (In fact, the styles
  901. **    should be linked to the whole stack not just the top one.)
  902. **    TBL 921119
  903. **
  904. **    We don't turn on "CAREFUL" check because the parser produces
  905. **    (internal code errors apart) good nesting. The parser checks
  906. **    incoming code errors, not this module.
  907. */
  908. PRIVATE void HTML_end_element ARGS2(HTStructured *, me, int , element_number)
  909. {
  910. #ifdef CAREFUL            /* parser assumed to produce good nesting */
  911.     if (element_number != me->sp[0].tag_number) {
  912.     fprintf(stderr, "HTMLText: end of element %s when expecting end of %s\n",
  913.         HTML_dtd.tags[element_number].name,
  914.         HTML_dtd.tags[me->sp->tag_number].name);
  915.         /* panic */
  916.     }
  917. #endif
  918.  
  919.     me->sp++;                /* Pop state off stack */
  920.  
  921.     switch(element_number) {
  922.  
  923.     /*
  924.      *    Forms stuff.
  925.      */
  926.     case HTML_FORM:    {
  927.         /*
  928.          *    Just call GridText to end the current form.
  929.          *    Be sure to update the style.
  930.          */
  931.         UPDATE_STYLE;
  932.         HText_endForm();
  933.         break;
  934.     }
  935.  
  936.     case HTML_A:
  937.     /*
  938.      *    First, check to see if low on memory.  If so, do not allow
  939.      *    any more anchors.  Be sure to wrap up the final anchor created
  940.      *    though.
  941.      */
  942.     if(really_safe_pool[Stop_Anchors] == NULL && c_DidStartAnchor == 0)
  943.     {
  944.         break;
  945.     }
  946.     /*
  947.      *    Mark for start of allowing no more of ending anchors if low
  948.      *    on memory.
  949.      */
  950.     c_DidStartAnchor = 0;
  951.     UPDATE_STYLE;
  952.     HText_endAnchor(me->text);
  953.     break;
  954.  
  955.     case HTML_TITLE:
  956.     HTChunkTerminate(&me->title);
  957.     HTAnchor_setTitle(me->node_anchor, me->title.data);
  958.     break;
  959.  
  960.     case HTML_LISTING:                /* Litteral text */
  961.     case HTML_XMP:
  962.     case HTML_PLAINTEXT:
  963.     case HTML_PRE:
  964.     if (me->comment_start)
  965.         HText_appendText(me->text, me->comment_start);
  966.     /* Fall through */
  967.  
  968.     default:
  969.  
  970.     change_paragraph_style(me, me->sp->style);    /* Often won't really change */
  971.     break;
  972.  
  973.     } /* switch */
  974. }
  975.  
  976.  
  977. /*        Expanding entities
  978. **        ------------------
  979. */
  980. /*    (In fact, they all shrink!)
  981. */
  982.  
  983. PRIVATE void HTML_put_entity ARGS2(HTStructured *, me, int, entity_number)
  984. {
  985.     HTML_put_string(me, ISO_Latin1[entity_number]);    /* @@ Other representations */
  986. }
  987.  
  988.  
  989. /*    Free an HTML object
  990. **    -------------------
  991. **
  992. ** If the document is empty, the text object will not yet exist.
  993.    So we could in fact abandon creating the document and return
  994.    an error code.  In fact an empty document is an important type
  995.    of document, so we don't.
  996. **
  997. **    If non-interactive, everything is freed off.   No: crashes -listrefs
  998. **    Otherwise, the interactive object is left.    
  999. */
  1000. PUBLIC void HTML_free ARGS1(HTStructured *, me)
  1001. {
  1002.     UPDATE_STYLE;        /* Creates empty document here! */
  1003.     if (me->comment_end)
  1004.         HTML_put_string(me,me->comment_end);
  1005.     HText_endAppend(me->text);
  1006.  
  1007.     if (me->target) {
  1008.         (*me->targetClass.free)(me->target);
  1009.     }
  1010.     free(me);
  1011. }
  1012.  
  1013.  
  1014. PRIVATE void HTML_abort ARGS2(HTStructured *, me, HTError, e)
  1015.  
  1016. {
  1017.     if (me->target) {
  1018.         (*me->targetClass.abort)(me->target, e);
  1019.     }
  1020.     free(me);
  1021.  
  1022. }
  1023.  
  1024.  
  1025. /*    Get Styles from style sheet
  1026. **    ---------------------------
  1027. */
  1028. PRIVATE void get_styles NOARGS
  1029. {
  1030.     got_styles = YES;
  1031.  
  1032.     default_style =        HTStyleNamed(styleSheet, "Normal");
  1033.  
  1034.     styles[HTML_H1] =        HTStyleNamed(styleSheet, "Heading1");
  1035.     styles[HTML_H2] =        HTStyleNamed(styleSheet, "Heading2");
  1036.     styles[HTML_H3] =        HTStyleNamed(styleSheet, "Heading3");
  1037.     styles[HTML_H4] =        HTStyleNamed(styleSheet, "Heading4");
  1038.     styles[HTML_H5] =        HTStyleNamed(styleSheet, "Heading5");
  1039.     styles[HTML_H6] =        HTStyleNamed(styleSheet, "Heading6");
  1040.     styles[HTML_H7] =        HTStyleNamed(styleSheet, "Heading7");
  1041.  
  1042.     styles[HTML_DL] =        HTStyleNamed(styleSheet, "Glossary");
  1043.     styles[HTML_UL] =
  1044.     styles[HTML_OL] =        HTStyleNamed(styleSheet, "List");
  1045.     styles[HTML_MENU] =        HTStyleNamed(styleSheet, "Menu");
  1046.     styles[HTML_DIR] =        HTStyleNamed(styleSheet, "Dir");    
  1047.     styles[HTML_DLC] =        HTStyleNamed(styleSheet, "GlossaryCompact");
  1048.     styles[HTML_ADDRESS]=    HTStyleNamed(styleSheet, "Address");
  1049.     styles[HTML_BLOCKQUOTE]=    HTStyleNamed(styleSheet, "Blockquote");
  1050.     styles[HTML_PLAINTEXT] =
  1051.     styles[HTML_XMP] =        HTStyleNamed(styleSheet, "Example");
  1052.     styles[HTML_PRE] =        HTStyleNamed(styleSheet, "Preformatted");
  1053.     styles[HTML_LISTING] =    HTStyleNamed(styleSheet, "Listing");
  1054. }
  1055. /*                P U B L I C
  1056. */
  1057.  
  1058. /*    Structured Object Class
  1059. **    -----------------------
  1060. */
  1061. PUBLIC CONST HTStructuredClass HTMLPresentation = /* As opposed to print etc */
  1062. {        
  1063.     "text/html",
  1064.     HTML_free,
  1065.     HTML_abort,
  1066.     HTML_put_character,     HTML_put_string,  HTML_write,
  1067.     HTML_start_element,     HTML_end_element,
  1068.     HTML_put_entity
  1069. }; 
  1070.  
  1071.  
  1072. /*        New Structured Text object
  1073. **        --------------------------
  1074. **
  1075. **    The strutcured stream can generate either presentation,
  1076. **    or plain text, or HTML.
  1077. */
  1078. PUBLIC HTStructured* HTML_new ARGS3(
  1079.     HTParentAnchor *,     anchor,
  1080.     HTFormat,        format_out,
  1081.     HTStream*,        stream)
  1082. {
  1083.  
  1084.     HTStructured * me;
  1085.     
  1086.     if (format_out != WWW_PLAINTEXT && format_out != WWW_PRESENT) {
  1087.         HTStream * intermediate = HTStreamStack(WWW_HTML, format_out,
  1088.         stream, anchor);
  1089.     if (intermediate) return HTMLGenerator(intermediate);
  1090.         fprintf(stderr, "** Internal error: can't parse HTML to %s\n",
  1091.         HTAtom_name(format_out));
  1092. #ifndef MSDOS
  1093.     exit (-99);
  1094. #else
  1095.     fprintf(stderr, "WWW html.c exit(-99); denied by doslynx.\n");
  1096.     fprintf(stderr, "Consider leaving the application.\n");
  1097. #endif /* MSDOS */
  1098.     }
  1099.  
  1100.     me = (HTStructured*) malloc(sizeof(*me));
  1101.     if (me == NULL) outofmem(__FILE__, "HTML_new");
  1102.  
  1103.     if (!got_styles) get_styles();
  1104.  
  1105.     me->isa = &HTMLPresentation;
  1106.     me->node_anchor =  anchor;
  1107.     me->title.size = 0;
  1108.     me->title.growby = 128;
  1109.     me->title.allocated = 0;
  1110.     me->title.data = 0;
  1111.     me->text = 0;
  1112.     me->style_change = YES; /* Force check leading to text creation */
  1113.     me->new_style = default_style;
  1114.     me->old_style = 0;
  1115.     me->sp = me->stack + MAX_NESTING - 1;
  1116.     me->sp->tag_number = -1;                /* INVALID */
  1117.     me->sp->style = default_style;            /* INVALID */
  1118.     
  1119.     me->comment_start = NULL;
  1120.     me->comment_end = NULL;
  1121.     me->target = stream;
  1122.     if (stream) me->targetClass = *stream->isa;    /* Copy pointers */
  1123.  
  1124.     return (HTStructured*) me;
  1125. }
  1126.  
  1127.  
  1128. /*    HTConverter for HTML to plain text
  1129. **    ----------------------------------
  1130. **
  1131. **    This will convert from HTML to presentation or plain text.
  1132. */
  1133. PUBLIC HTStream* HTMLToPlain ARGS3(
  1134.     HTPresentation *,    pres,
  1135.     HTParentAnchor *,    anchor,    
  1136.     HTStream *,        sink)
  1137. {
  1138.     return SGML_new(&HTML_dtd, HTML_new(anchor, pres->rep_out, sink));
  1139. }
  1140.  
  1141.  
  1142. /*    HTConverter for HTML to C code
  1143. **    ------------------------------
  1144. **
  1145. **    C copde is like plain text but all non-preformatted code
  1146. **    is commented out.
  1147. **    This will convert from HTML to presentation or plain text.
  1148. */
  1149. PUBLIC HTStream* HTMLToC ARGS3(
  1150.     HTPresentation *,    pres,
  1151.     HTParentAnchor *,    anchor,    
  1152.     HTStream *,        sink)
  1153. {
  1154.     
  1155.     HTStructured * html;
  1156.     
  1157.     (*sink->isa->put_string)(sink, "/* ");    /* Before even title */
  1158.     html = HTML_new(anchor, WWW_PLAINTEXT, sink);
  1159.     html->comment_start = "/* ";
  1160.     html->comment_end = " */\n";    /* Must start in col 1 for cpp */
  1161. /*    HTML_put_string(html,html->comment_start); */
  1162.     return SGML_new(&HTML_dtd, html);
  1163. }
  1164.  
  1165.  
  1166. /*    Presenter for HTML
  1167. **    ------------------
  1168. **
  1169. **    This will convert from HTML to presentation or plain text.
  1170. **
  1171. **    Override this if you have a windows version
  1172. */
  1173. #ifndef GUI
  1174. PUBLIC HTStream* HTMLPresent ARGS3(
  1175.     HTPresentation *,    pres,
  1176.     HTParentAnchor *,    anchor,    
  1177.     HTStream *,        sink)
  1178. {
  1179.     return SGML_new(&HTML_dtd, HTML_new(anchor, WWW_PRESENT, sink));
  1180. }
  1181. #endif
  1182.  
  1183.  
  1184. /*    Record error message as a hypertext object
  1185. **    ------------------------------------------
  1186. **
  1187. **    The error message should be marked as an error so that
  1188. **    it can be reloaded later.
  1189. **    This implementation just throws up an error message
  1190. **    and leaves the document unloaded.
  1191. **    A smarter implementation would load an error document,
  1192. **    marking at such so that it is retried on reload.
  1193. **
  1194. ** On entry,
  1195. **    sink     is a stream to the output device if any
  1196. **    number    is the HTTP error number
  1197. **    message    is the human readable message.
  1198. **
  1199. ** On exit,
  1200. **    returns    a negative number to indicate lack of success in the load.
  1201. */
  1202.  
  1203. PUBLIC int HTLoadError ARGS3(
  1204.     HTStream *,     sink,
  1205.     int,        number,
  1206.     CONST char *,    message)
  1207. {
  1208.     HTAlert(message);        /* @@@@@@@@@@@@@@@@@@@ */
  1209.     return -number;
  1210. }
  1211.  
  1212.